﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace WpfApp1
{
    /// <summary>
    /// Window4.xaml 的交互逻辑
    /// </summary>
    public partial class Window4 : Window
    {
         
        public System.Timers.Timer timer;
        string Terminator = "*\r";
        public string Name;
        public string IP;
        public int Port;
        object locker = new object();
        public Window4()
        {
            InitializeComponent();
        }

        public string FCS(string data)
        {
            int xorresult = 0;
            string tempfes = "";
            for (int i = 0; i < data.Length; i++)
            {
                xorresult = xorresult ^ Convert.ToInt32(data[i]);
            }
            tempfes = Convert.ToString(xorresult, 16);
            if (tempfes.Length == 1)
            {
                tempfes = "0" + tempfes;
            }
            return tempfes;
        }
        public string PackCmd(string cmd)
        {
            cmd = string.Format("@00{0}", cmd);
            var fcs = FCS(cmd).ToUpper();
            cmd = string.Format("{0}{1}{2}", cmd, fcs, Terminator);
            return cmd;
        }
        Socket s1;
        string ip;
        int port;
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                s1 = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                ip = txtIp.Text.Trim();
                port = int.Parse(txtPort.Text.Trim());
                s1.Connect(ip, port);
                Console.WriteLine(s1.Connected);
                var c = txtCmd1.Text.Trim();
                var fcs = FCS(c);
                var cmd = string.Format("{0}{1}{2}", c, fcs, "*\r");
                Console.WriteLine(cmd);
                var b = Encoding.Default.GetBytes(cmd);
                s1.Send(b);
                Console.WriteLine("send");
                s1.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);

            }
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            try
            {
                s1 = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                ip = txtIp.Text.Trim();
                port = int.Parse(txtPort.Text.Trim());
                s1.Connect(ip, port);
                Console.WriteLine(s1.Connected);
                var c = txtCmd1.Text.Trim();
                var fcs = FCS(c);
                var cmd = string.Format("{0}{1}{2}", c, fcs, "*\r");
                Console.WriteLine(cmd);
                var b = Encoding.Default.GetBytes(cmd);
                s1.Send(b);
                Console.WriteLine("send");
                s1.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);

            }

        }
    }
}
